package com.innovations.inn.controller;

import com.innovations.common.annotation.Log;
import com.innovations.common.core.controller.BaseController;
import com.innovations.common.core.domain.AjaxResult;
import com.innovations.common.core.page.TableDataInfo;
import com.innovations.common.enums.BusinessType;
import com.innovations.common.utils.poi.ExcelUtil;
import com.innovations.framework.web.service.TokenService;
import com.innovations.inn.domain.UserAttention;
import com.innovations.inn.service.IUserAttentionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 用户关注信息Controller
 *
 * @author dp
 * @date 2024-08-12
 */
@Api(tags = "用户关注信息")
@RestController
@RequestMapping("/inn/attention")
public class UserAttentionController extends BaseController {
    @Autowired
    private IUserAttentionService userAttentionService;
    @Autowired
    TokenService tokenService;

/**
 * 查询用户关注信息列表
 */
@ApiOperation("查询用户关注信息列表")
@PreAuthorize("@ss.hasPermi('inn:attention:list')")
@GetMapping("/list")
    public TableDataInfo list(UserAttention userAttention) {
        startPage();
        List<UserAttention> list = userAttentionService.selectUserAttentionList(userAttention);
        return getDataTable(list);
    }

    /**
     * 导出用户关注信息列表
     */
    @PreAuthorize("@ss.hasPermi('inn:attention:export')")
    @Log(title = "用户关注信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UserAttention userAttention) {
        List<UserAttention> list = userAttentionService.selectUserAttentionList(userAttention);
        ExcelUtil<UserAttention> util = new ExcelUtil<UserAttention>(UserAttention. class);
        util.exportExcel(response, list, "用户关注信息数据");
    }

    /**
     * 获取用户关注信息详细信息
     */
    @ApiOperation("获取用户关注信息详细信息")
    @PreAuthorize("@ss.hasPermi('inn:attention:query')")
    @GetMapping(value = "/{attentionId}")
    public AjaxResult getInfo(@PathVariable("attentionId") Long attentionId) {
        return success(userAttentionService.selectUserAttentionByAttentionId(attentionId));
    }

    /**
     * 新增用户关注信息
     */
    @ApiOperation("新增用户关注信息")
    @PreAuthorize("@ss.hasPermi('inn:attention:add')")
    @Log(title = "用户关注信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(HttpServletRequest request, @RequestBody UserAttention userAttention) {
        Long userId = tokenService.getLoginUser(request).getUser().getUserId();
        userAttentionService.completeData(userAttention, userId, 1);
        return toAjax(userAttentionService.insertUserAttention(userAttention));
    }

    /**
     * 修改用户关注信息
     */
    @ApiOperation("修改用户关注信息")
    @PreAuthorize("@ss.hasPermi('inn:attention:edit')")
    @Log(title = "用户关注信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(HttpServletRequest request, @RequestBody UserAttention userAttention) {
        Long userId = tokenService.getLoginUser(request).getUser().getUserId();
        userAttentionService.completeData(userAttention, userId, 2);
        return toAjax(userAttentionService.updateUserAttention(userAttention));
    }

    /**
     * 删除用户关注信息
     */
    @ApiOperation("删除用户关注信息")
    @PreAuthorize("@ss.hasPermi('inn:attention:remove')")
    @Log(title = "用户关注信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{attentionIds}")
    public AjaxResult remove(@PathVariable Long[] attentionIds) {
        return toAjax(userAttentionService.deleteUserAttentionByAttentionIds(attentionIds));
    }
}
